package com.example.tool.adapter.base;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import java.util.List;

/**
 * Created Date 2019/8/25.
 *
 * @author liangkun
 * ClassDescription：通用适配器
 */
public abstract class CommonAdapter<T> extends RecyclerView.Adapter<CommonViewHolder> {
    protected Context mContext;
    protected List<T> mDatas;
    private int mLayoutId;
    protected OnItemClickListener mOnItemClickListener;


    public CommonAdapter(Context context, List<T> datas, int layoutId) {
        mContext = context;
        mDatas = datas;
        this.mLayoutId = layoutId;
    }

    public void updataData(List<T> datas) {
        mDatas.clear();
        mDatas = datas;
    }

    @Override
    public CommonViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(mLayoutId, parent, false);
        return new CommonViewHolder(mContext, view);
    }


    @Override
    public void onBindViewHolder(final CommonViewHolder holder, final int position) {
        holder.itemView.setTag(position);
        T bean = mDatas.get(position);
        onBindData(holder, bean, position);
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }


    public List<T> getDatas() {
        return mDatas;
    }


    protected abstract void onBindData(CommonViewHolder holder, T bean, int position);


    public interface OnItemClickListener {
        void onItemClick(View view, CommonViewHolder holder, int position);

        boolean onItemLongClick(View view, CommonViewHolder holder, int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

}
